<?php
namespace app\admin\controller;
use app\admin\model\Dyongjin;
use app\admin\model\User as Usermodel;
use app\admin\model\Payhistory;
use app\admin\model\Uyongjin;

class User extends Base
{
    /**
     * 用户列表
     * @return \think\response\View
     */
    public function index(Usermodel $user){
        $nowpage = I('param.page');
        if(!$nowpage){
            $nowpage=1;
        }
        $ispage=true;
        $data = input();
        $where = [];
        if(!empty($data['black'])) {
            $ispage=false;
            $where['is_black'] = 1;
        }
        if(!empty($data['sci'])) {
            $ispage=false;
            $where['id'] = $data['sci'];
        }
        //查询用户信息
       
        if ($ispage){
            $list = $user->where($where)->page($nowpage,10)->select();
        }else {
            $list = $user->where($where)->select();
        }
        //查询该用户是否存在充值记录
        $pay = new Payhistory();
        $yongjin = new Uyongjin();
        $tuijian = new \app\index\model\User();
        $paymoney = 0;
        $yjmoney = 0;
        $commoney = 0;
        $canmoney = 0;
        $tmp = '';
        foreach ($list as $key=>$val) {
            $tmp[$key]['id'] = $val['id'];
            $tmp[$key]['openid'] = $val['openid'];
            $tmp[$key]['nickname'] = $val['nickname'];
            $tmp[$key]['imgurl'] = $val['imgurl'];
            $tmp[$key]['money'] = $val['money'];
            $tmp[$key]['is_black'] = $val['is_black'];
            $tmp[$key]['is_daili'] = $val['is_daili'];
            $tmp[$key]['is_usedefault'] = $val['is_usedefault'];
            $rel = $pay->where('user_id',$val['id'])->where('status',1)->select();
            if($rel) {
                //整合该用户充值记录
                $paymoney = 0;
                foreach ($rel as $k=>$v) {
                    $paymoney += $v['money'];
                }
                $tmp[$key]['paynum'] = $paymoney;
            }else {
                $tmp[$key]['paynum'] = 0;
            }

            //查询当前用户是否有佣金
            $res = $yongjin->where('user_id',$val['id'])->select();
            if($res) {
            //计算总佣金
                $yjmoney = 0;
                foreach($res as $x=>$y) {
                    $yjmoney += $y['money'];
                }
                $tmp[$key]['yongjin'] = $yjmoney;
                //统计已发放佣金
                $com = $yongjin->where('user_id',$val['id'])->where('is_fa',1)->select();
                if($com) {
                    $commoney = 0;
                    foreach($com as $a=>$b) {
                        $commoney += $b['money'];
                    }
                    $tmp[$key]['com'] = $commoney;
                }else {
                    $tmp[$key]['com'] = 0;
                }

                //统计未发放佣金
                $can = $yongjin->where('user_id',$val['id'])->where('is_fa',0)->select();
                if($can) {
                    $canmoney = 0;
                    foreach ($can as $c => $d) {
                        $canmoney += $d['money'];
                    }
                    $tmp[$key]['can'] = $canmoney;
                }else {
                    $tmp[$key]['can'] = 0;
                }
            }else {
                //当前用户无佣金记录
                $tmp[$key]['yongjin'] = 0;
                $tmp[$key]['com'] = 0;
                $tmp[$key]['can'] = 0;
            }
            $tmp[$key]['tuijian']=$tuijian->gettuijianCount($val['id']);
            
            
            
        }
        $num = count($tmp);
        if(!empty($data['yj']) && $data['yj'] == 1) {
            $arr = array_sort($tmp,'yongjin',false);
            $this->assign('list',$arr);
        }elseif (!empty($data['cz']) && $data['cz'] == 1) {
            $arr = array_sort($tmp,'paynum',false);
            $this->assign('list',$arr);
        }else {
            $this->assign('list',$tmp);
        }
        $prepage=$nowpage-1;
        if ($nowpage==1){
            $prepage=0;
        }
        $this->assign("prepage",$prepage);
        $this->assign("page",$num);
        $this->assign("nowpage",$nowpage);
        $nextpage=$nowpage+1;
        if ($num<10){
            $nextpage = 0;
        }
        $this->assign("nextpage",$nextpage);
        return view();
    }

    /**
     * 是否开启代理
     */
    public function agent(Usermodel $user)
    {
        $data = input();
        if(empty($data['id']) || empty($data['flag'])) {
            return $this->param_error();
        }

        if($data['flag'] == 1) {
            //开启
            $rel = $user->where('id',$data['id'])->update(['is_daili'=>1]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError();
            }
        }

        if($data['flag'] == 2) {
            //关闭
            $rel = $user->where('id',$data['id'])->update(['is_daili'=>0]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError();
            }
        }
    }

    /**
     * 是否开启代理自定义配置
     */
    public function defaultAgent(Usermodel $user)
    {
        $data = input();
        if(empty($data['id']) || empty($data['flag'])) {
            return $this->param_error();
        }
        if($data['flag'] == 2) {
            //开启自定义配置
            $rel = $user->where('id',$data['id'])->update(['is_usedefault'=>0]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError('开启失败');
            }
        }

        if($data['flag'] == 1) {
            //开启默认设置
            $rel = $user->where('id',$data['id'])->update(['is_usedefault'=>1]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError('开启失败');
            }
        }
    }

    /**
     * 黑名单
     */
    public function forbid(Usermodel $user)
    {
        $data = input();
        if(empty($data['id']) || empty($data['flag'])) {
            return $this->param_error();
        }

        if($data['flag'] == 1) {
            //加入黑名单
            $rel = $user->where('id',$data['id'])->update(['is_black'=>1]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError();
            }
        }

        if($data['flag'] == 2) {
            //去除黑名单
            $rel = $user->where('id',$data['id'])->update(['is_black'=>0]);
            if($rel) {
                return $this->returnOk();
            }else {
                return $this->returnError();
            }
        }
    }

    /**
     * 充值记录
     * @return \think\response\View
     */
    public function pay()
    {
        $nowpage = I('param.page');
        $ispage=true;
        $data = input();
        $where = [];
        $time = [];
        if(!empty($data['sci'])) {
            $ispage=false;
            $where['p.user_id'] = $data['sci'];
        }
        if(!empty($data['sktime']) && !empty($data['sjtime'])) {
            $ispage=false;
            $time[] = $data['sktime'];
            $time[] = $data['sjtime'];
            $where['p.create_time'] = ['between',$time];
        }
        if(!$nowpage){
            $nowpage=1;
        }
//        $pay = new Payhistory();
        $pay = D('payhistory');
        
        if ($ispage){
            $list = $pay->alias('p')
                    ->field('p.user_id,p.money,p.create_time,u.nickname,u.imgurl,p.order_id')
                    ->join('user u','u.id=p.user_id')
                    ->order('p.create_time','desc')
                    ->where($where)
                    ->where('p.enable',0)
                    ->where('p.status',1)
                    ->page($nowpage,10)
                    ->select();
        }else {
           $list = $pay->alias('p')
                    ->field('p.user_id,p.money,p.create_time,u.nickname,u.imgurl,p.order_id')
                    ->join('user u','u.id=p.user_id')
                    ->order('p.create_time','desc')
                    ->where($where)
                    ->where('p.enable',0)
                    ->where('p.status',1)
                    ->select();
        }
        
        $num = count($list);
        $this->assign('list',$list);
        $prepage=$nowpage-1;
        if ($nowpage==1){
            $prepage=0;
        }
        $this->assign("prepage",$prepage);
        $this->assign("page",$num);
        $this->assign("nowpage",$nowpage);
        $nextpage=$nowpage+1;
        if ($num<10){
            $nextpage = 0;
        }
        $this->assign("nextpage",$nextpage);
        return view();
    }
    /**
     * 佣金列表
     * @return \think\response\View
     */
//    function yongjin(){
//        $this->assign('page',1);
//        return view();
//    }
    /**
     * 未结算佣金列表
     * @return \think\response\View
     */
    public function tixian(){
//        $user = new Usermodel();
        $nowpage = I('param.page');
        $ispage=true;
        $user = D('user');//此处要这么用
        $data = input();
        $where = [];
        if(!empty($data['sci'])) {
            $ispage=false;
            $where['y.user_id'] = $data['sci'];
        }
        if(!$nowpage){
            $nowpage=1;
        }
        
        if ($ispage){
            //查询一个用户未结算的佣金
        $list = $user->alias('u')
                     ->field('y.user_id,u.openid,u.nickname,u.imgurl,sum(y.money) as money,y.create_time')
                     ->join('uyongjin y','u.id=y.user_id')
                     ->where('y.is_fa',0)
                     ->where($where)
                     ->order('y.create_time','desc')
                     ->group('y.user_id')
                     ->page($nowpage,10)
                     ->select();
        }else {
            //查询一个用户未结算的佣金
        $list = $user->alias('u')
                     ->field('y.user_id,u.openid,u.nickname,u.imgurl,sum(y.money) as money,y.create_time')
                     ->join('uyongjin y','u.id=y.user_id')
                     ->where('y.is_fa',0)
                     ->where($where)
                     ->order('y.create_time','desc')
                     ->group('y.user_id')
                     ->select();
        }
        
        $num = count($list);
        $this->assign('list',$list);
        $prepage=$nowpage-1;
        if ($nowpage==1){
            $prepage=0;
        }
        $this->assign("prepage",$prepage);
        $this->assign("page",$num);
        $this->assign("nowpage",$nowpage);
        $nextpage=$nowpage+1;
        if ($num<10){
            $nextpage = 0;
        }
        $this->assign("nextpage",$nextpage);
        return view();
    }

    /**
     * 佣金结算
     */
    public function count()
    {
        $data = input();
        $where = [];
        $mon = new Uyongjin();
        if(!empty($data['id'])) {
            $where['user_id'] = $data['id'];
            $mon->yongjinduihuan($this->jcuserId,$where['user_id']);
        }else{
            $mon->yongjinduihuan($this->jcuserId);
        }
        
        //统一结算
        $list = $mon->where($where)->where('is_fa',0)->update(['is_fa'=>1]);
        if($list) {
            $this->redirect("User/tixian");
        }else {
            return $this->returnError('结算失败');
        }
    }

    /**
     * 代理自定义配置
     */
    public function user()
    {
        $data =  input();
        $uid = $data['id'];
        //首先判断当前代理是否存在自定义配置
        $dyj = new Dyongjin();
        $rel = $dyj->where('user_id',$uid)->select();
        $list = '';
        if(empty($rel)) {
            //显示默认代理配置
            $list = $dyj->where('isalone',0)->select();
            $this->assign('list',$list);
        }else {
            $this->assign('list',$rel);
        }
        $this->assign('uid',$uid);
        return view();
    }

    /**
     * 保存代理自定义配置
     */
    public function addSelf()
    {
        $data = input();
        //首先应判断是否有自定义配置，如果有则更新，没有则新增
        $dyj = new Dyongjin();
        $list = $dyj->where('user_id',$data['uid'])->select();

        //改变用户佣金为自定义配置
        $user = new Usermodel();
        $user->where('id',$data['uid'])->update(['is_usedefault'=>0]);
        if($list) {
            //更新
            $arr = $data['hid'];
            $arr1 = $data['auths'];
            $arr2 = $data['b'];
            foreach($arr as $key=>$val) {
                $item['id'] = $arr[$key];
                $item['type'] = $arr1[$val];
                $item['baifenbi'] = $arr2[$val];
                $item['user_id'] = $data['uid'];
                $item['isalone'] = 1;
                $dyj->update($item);
            }
            $this->redirect('User/user',['id'=>$data['uid']]);

        }else {
            //新增
            $arr = $data['hid'];
            $arr1 = $data['auths'];
            $arr2 = $data['b'];
            foreach($arr as $key=>$val) {
                $item['type'] = $arr1[$val];
                $item['baifenbi'] = $arr2[$val];
                $item['user_id'] = $data['uid'];
                $item['isalone'] = 1;
                $dyj->insert($item);
            }
            $this->redirect('User/user',['id'=>$data['uid']]);
        }
    }
    
    function yue(){
        $nowpage = I('param.page');
        $ispage=true;
        $user  = D("user");
        $sci = I('param.sci');
        if(!empty($sci)) {
            $ispage=false;
            $where['id'] = $sci;
        }
        if(!$nowpage){
            $nowpage=1;
        }
        $where['money']=[">",0];
        if ($ispage){
            $list  = $user->where($where)->page($nowpage,10)->select();
        }else {
            $list  = $user->where($where)->select();
        }
        
        $this->assign('list',$list);
        $num = count($list);
        $prepage=$nowpage-1;
        if ($nowpage==1){
            $prepage=0;
        }
        $this->assign("prepage",$prepage);
        $this->assign("page",$num);
        $this->assign("nowpage",$nowpage);
        $nextpage=$nowpage+1;
        if ($num<10){
            $nextpage = 0;
        }
        $this->assign("nextpage",$nextpage);
        return view();
    }
    function yuejiesuan(){
        $id = I("param.id");
        if (!$id){
            return $this->error("结算出错");
        }
        
        $user  = D("user");
        $res = $user->where("id",$id)->find();
        if(!$res){
            return $this->error("未找到此用户");
        }
        if($res['money']<=0){
            return $this->error("用户余额为0，不能结算");
        }
        $money = $res['money'];
        $res['money']=0;
        $user->update($res);
        $duihuan = D("duihuan");
        $duihuanData['user_id'] = $id;
        $duihuanData['hand_id'] = $this->jcuserId;
        $duihuanData['money'] = $money;
        $duihuanData['type'] = 1;
        $duihuan->insert($duihuanData);
        $this->redirect("User/yue");
    }
    
    function tuijian(){
        $type = I("param.type");
        $id = I("param.id");
        if (!$id){
            $this->error("查询出错");
        }
        if (!$type){
            $type=1;
        }
        $tuijian = new \app\index\model\User();
        $data = $tuijian->getMyTuiJianAll($id);
        $list = [];
        if ($type==1){
            
            $list = $data[0];
            
            $this->assign('curr_type_name',"一级");
        }else if($type==2){
            $list = $data[1];
            $this->assign('curr_type_name',"二级");
        }else{
            $list = $data[2];
            $this->assign('curr_type_name',"三级");
        }
        
        $this->assign("list",$list);
        $this->assign('user_id',$id);
        $this->assign('page',count($list));
        
        
        return view();
    }
    function jchistory() {
        $id = I('param.id');
        if (!$id){
            return $this->error("出错");
        }
        $jc = new \app\index\model\Jc();
        
        $data = $jc->getMyJCAll($id);
        $this->assign('list',$data);
        $this->assign('page',count($data));
        return view();
    }

    function jchistory2() {
        $id = I('param.id');
        $user_id = I('param.user_id');
        if (!$id){
            return $this->error("出错");
        }
        $jc = new \app\index\model\Jc();
        
        $data = $jc->getMyJCAll($id);
        $this->assign('list',$data);
        $this->assign('page',count($data));
        $this->assign('user_id',$user_id);
    return view();
    }
}
